var express = require('express');
var router = express.Router();

var DB = require("../../model/db");
var md5 = require("md5-node");
var async = require("async");
var multiparty = require('multiparty');
var sd = require('silly-datetime');
var bodyParser = require('body-parser');
router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: false }));

var Host = "";

router.use(function (req, res, next) {
    Host = "http://" + req.headers.host;
    next();
})
/* GET users listing. */

router.get('/', function (req, res) {
    var page = req.query.page;
    if (!page) {
        page = 1;
    }
    async.parallel({
        count: function (callback) {
            DB.count("user", {}, function (err, data) {
                callback(err, data)
            })
        },
        user: function (callback) {
            DB.find("user", {}, { pageSize: 5, page: page }, function (err, data) {
                callback(err, data)
            })
        }
    }, function (err, data) {
        res.render("admin/user/index", {
            host: Host,
            list: data.user,
            page:page,
            count: Math.ceil(data.count / 5)
        })
    })
});


router.get('/add', function (req, res, next) {
    res.render("admin/user/add", {
        host: Host
    })
});


router.post('/addPost', function (req, res) {
    var form = new multiparty.Form();
    form.uploadDir = "./public/resource";
    form.parse(req, function (err, fields, files) {
        var username = fields.username[0];
        var password = md5(fields.password[0]);
        var tel = fields.tel[0];
        var address = fields.address[0];
        var email = fields.email[0];
        var status = fields.status[0];
        var addtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
        DB.insertOne("user", { username: username, password: password, tel: tel,address:address, email: email, status: status, addtime: addtime,  }, function (err, result) {
            if (err) {
                console.log(err);
                return;
            } else {
                res.send('<script>alert("添加成功");location.href="/admin/user"</script>');
            }
        })

    });
});


router.get('/edit/:id', function (req, res, next) {
    var id = req.params.id;
    DB.find("user", { "_id": new DB.ObjectID(id) }, function (err, data) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.render("admin/user/edit", {
                host: Host,
                list: data
            })
        }
    })
});

router.post('/editPost', function (req, res) {
    var form = new multiparty.Form();
    form.uploadDir = "./public/resource";
    form.parse(req, function (err, fields, files) {
        var username = fields.username[0];
        var password = md5(fields.password[0]);
        var tel = fields.tel[0];
        var address = fields.address[0];
        var email = fields.email[0];
        var status = fields.status[0];
        var id = fields.id[0];
        var addtime = sd.format(new Date(), 'YYYY-MM-DD HH:mm');
        DB.updateOne("user", { "_id": new DB.ObjectID(id) }, { username: username, password: password, tel: tel,address:address, email: email, status: status, addtime: addtime,  }, function (err, rusult) {
            if (err) {
                console.log(err);
                return;
            } else {
                res.send('<script>alert("修改成功");location.href="/admin/user"</script>');
            }
        })
    });


});




router.get('/delete/:id', function (req, res, next) {
    var id = req.params.id;
    DB.deleteMany("user", { "_id": new DB.ObjectID(id) }, function (err, result) {
        if (err) {
            console.log(err);
            return;
        } else {
            res.redirect(Host + "/admin/user");
        }
    })
});

module.exports = router;